package com.qianfeng.dbsm.admin.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianfeng.dbsm.admin.dao.TbMemberDao;
import com.qianfeng.dbsm.admin.entity.TbMember;
import com.qianfeng.dbsm.admin.service.TbMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 会员表(TbMember)表服务实现类
 *
 * @author makejava
 * @since 2022-11-14 19:33:40
 */
@Service("tbMemberService")
public class TbMemberServiceImpl extends ServiceImpl<TbMemberDao, TbMember> implements TbMemberService ,UserDetailsService{

    @Autowired
    TbMemberDao tbMemberDao;

    //会员登录
    public TbMember login(String username, String password) {
        TbMember loginUser =  tbMemberDao.selectOne(new QueryWrapper<TbMember>().eq("member_account",username));
        if(loginUser!=null){
            if(loginUser.getMemberPassword().equals(password)){
                return loginUser;
            }
            throw new RuntimeException("密码不正确");
        }
        throw new RuntimeException("用户不存在");
    }

    //根据用户id查询一级上级用户
    public List<Integer> bossMembersById(Integer id) {
        List<Integer>list = tbMemberDao.bossMembersById(id);
        return list;
    }



    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        final TbMember loginUser = tbMemberDao.selectOne(new QueryWrapper<TbMember>().eq("member_account",s));

        if(loginUser!=null){
            //将自定义的user转换成security的指定的userDetails
            UserDetails userDetails = new UserDetails() {

                public Collection<? extends GrantedAuthority> getAuthorities() {
                    //获取权限
                    List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
                    list.add(new SimpleGrantedAuthority("user:add"));
                    list.add(new SimpleGrantedAuthority("user:query"));
                    return list;
                }


                public String getPassword() {
                    return loginUser.getMemberPassword();
                }


                public String getUsername() {
                    return loginUser.getMemberAccount();
                }


                public boolean isAccountNonExpired() {
                    return true;
                }


                public boolean isAccountNonLocked() {
                    return true;
                }


                public boolean isCredentialsNonExpired() {
                    return true;
                }


                public boolean isEnabled() {
                    return true;
                }
            };
            return userDetails;
        }
        return null;
    }
}

